package com.modele.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.modele.dao.DAO;
import com.modele.entity.TypeRestaurant;

public class TypeRestaurantDAO extends DAO<TypeRestaurant>{

	public TypeRestaurantDAO(Connection connexion) {
		super(connexion);
	}

	public boolean create(String type) {
		try {
			PreparedStatement statement = connexion.prepareStatement("INSERT INTO typeRestaurant " +
				"(type) " +
				"VALUES (?)");
			statement.setString(1, type);
			return statement.executeUpdate() == 1;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
		//type dans liste, bouton retour commentaire pour nouveau, nom type resto trop long dans nouveau, ajouter tel resto
		//ajouter carte
	}
	@Override
	public boolean create(TypeRestaurant obj) {
		try {
			PreparedStatement statement = connexion.prepareStatement("INSERT INTO typeRestaurant " +
				"(nom) " +
				"VALUES (?)");
			statement.setString(1, obj.getTypeResto());
			return statement.executeUpdate() == 1;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
		
	}
	@Override
	public ArrayList<TypeRestaurant> findAll() {
		ArrayList<TypeRestaurant> liste = new ArrayList<TypeRestaurant>() ; 
		try {
			PreparedStatement statement = connexion.prepareStatement("SELECT * FROM typeRestaurant " );
			ResultSet rs = statement.executeQuery();
			while(rs.next()){
				liste.add(new TypeRestaurant(rs.getInt("id"), rs.getString("type")));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return liste;
	}

	@Override
	public boolean delete(TypeRestaurant obj) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(TypeRestaurant obj) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public TypeRestaurant find(int id) 
	{
		TypeRestaurant typeRestaurant = null;

		try 
		{
			PreparedStatement statement = connexion.prepareStatement("SELECT * FROM typeRestaurant WHERE id = ?");
			statement.setInt(1, id);

			if(statement.execute())
			{
				ResultSet result = statement.getResultSet();
				if(result.next())
				{
					typeRestaurant = new TypeRestaurant(id, result.getString(2));
				}
			}
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return typeRestaurant;
	}
	public TypeRestaurant find(String nom) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public TypeRestaurant findByValue(Object obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isPresent(Object obj) {
		// TODO Auto-generated method stub
		return false;
	}


}
